/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package be.vdab.servlets;

import be.vdab.dao.KlantDAO;
import be.vdab.entiteit.Klant;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author tom.vanpoucke
 */
@WebServlet("/voorstellingen/inlog.htm")
public class InlogServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static final String VIEW = "/WEB-INF/JSP/inloggen.jsp";
    private static final String INPUT = "/voorstellingen/inlog.htm";
    private static final KlantDAO klantDAO = new KlantDAO();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        if(session != null){
            @SuppressWarnings("unchecked")
            Map<String, String> fouten = (Map<String, String>) session.getAttribute("fouten");
            session.removeAttribute("fouten");
            if(fouten == null){
                Klant klant = (Klant) session.getAttribute("klant");
                request.setAttribute("succes", klant);
            }
            else{
                request.setAttribute("fouten", fouten);
            }
            RequestDispatcher dispatcher = request.getRequestDispatcher(VIEW);
            dispatcher.forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession(false);
        if(session != null){
            @SuppressWarnings("unchecked")
            Map<String, String> fouten = new HashMap<String, String>();
            String gebruikersnaam = request.getParameter("gebruikersnaam");
            String paswoord = request.getParameter("paswoord");
            
            if(gebruikersnaam == null || gebruikersnaam.isEmpty()){
                fouten.put("gebruikersnaam","Gebruikersnaam niet ingevuld");
            }
            if(paswoord == null || paswoord.isEmpty()){
                fouten.put("paswoord","Paswoord niet ingevuld");
            }
            
            if(fouten.isEmpty()){
                Klant klant = klantDAO.findKlant(gebruikersnaam, paswoord);
                if(klant == null){
                    fouten.put("geenKlant", "Gebruikersnaam en paswoord komen niet overeen");
                    session.setAttribute("fouten", fouten);
                    response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + INPUT));               
                }
                else{
                    session.setAttribute("klant", klant);
                    response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + INPUT));
                }
            }
            else{
                session.setAttribute("fouten", fouten);
                response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + INPUT));
            }
        }
    }
}
